$(function () {
    edatagrid_01();
    edatagrid_02();
    edatagrid_03();
    edatagrid_04();
    //  加载配置
    $.ajax({
        url: 'sqlBalance/balanceForwary',
        traditional: true,
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            $('#v_grid_s').edatagrid('loadData', data.receivablesMent);
            $('#v_grid_f').edatagrid('loadData', data.receivablesAble);
            $('#v_grid_yl').edatagrid('loadData', data.materialOrders);
            let greyfabricStorage = [];
            for (let i = 0; i <data.greyfabricStorage.length; i++) {
                let element = data.greyfabricStorage[i];
                if (Number(element.ps) !== 0 || Number(element.qty) !== 0) {
                    greyfabricStorage.push(element);
                }
            }
            $('#v_grid_bp').edatagrid('loadData', greyfabricStorage);
        }
    });
})

//应收款
function edatagrid_01() {
    $('#v_grid_s').edatagrid({
        method: 'POST',
        param: {},
        showFooter:true,
        fit:true,
        columns: [[
            {
                field: 'billtype',
                title: '单据类型',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: false}}
            }, {
                field: 'client',
                title: '客户',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'money',
                title: '金额',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'numberbox', options: {required: true}}
            }, {
                field: 'billdate',
                title: '单据日期',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'billmemo',
                title: '备注',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }
        ]],
        onLoadSuccess: function (data) {
            let rows = data.rows;
            var money=0;
            for (let i = 0; i < rows.length; i++) {
                money += rows[i].money ? parseFloat(rows[i].money) : 0;
            }
            $(this).datagrid('reloadFooter', [{
                billtype: '合计',
                money:money.toFixed(2)
            }])
        },
    })
}

//应付款
function edatagrid_02() {
    $('#v_grid_f').edatagrid({
        method: 'POST',
        param: {},
        showFooter:true,
        fit:true,
        columns: [[
            {
                field: 'billtype',
                title: '单据类型',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: false}}
            }, {
                field: 'clientName',
                title: '客户',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'money',
                title: '金额',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'numberbox', options: {required: true}}
            }, {
                field: 'billDate',
                title: '单据日期',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'billmemo',
                title: '备注',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }
        ]],
        onLoadSuccess: function (data) {
            let rows = data.rows;
            var money=0;
            for (let i = 0; i < rows.length; i++) {
                money += rows[i].money ? parseFloat(rows[i].money) : 0;
            }
            $(this).datagrid('reloadFooter', [{
                billtype: '合计',
                money:money.toFixed(2)
            }])
        },
    })
}

//原料库存
function edatagrid_03() {
    $('#v_grid_yl').edatagrid({
        method: 'POST',
        param: {},
        showFooter:true,
        fit:true,
        columns: [[
            {
                field: 'orderDate',
                title: '制单日期',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: false}}
            }, {
                field: 'orderCode',
                title: '单据编号',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            },  {
                field: 'accountType',
                title: '付款类型',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'supplierName',
                title: '供应商',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'batchNum',
                title: '批次号',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'materialSpec',
                title: '原料规格',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'boxAmount',
                title: '箱数',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'numberbox', options: {required: true}}
            }, {
                field: 'inputKilo',
                title: '入库重量',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'numberbox', options: {required: true}}
            }, {
                field: 'remarks',
                title: '备注',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }
        ]],
        onLoadSuccess: function (data) {
            let rows = data.rows;
            var boxAmount=0;
            var inputKilo=0;
            var inputMoney=0;
            for (let i = 0; i < rows.length; i++) {
                boxAmount += rows[i].boxAmount ? parseFloat(rows[i].boxAmount) : 0;
                inputKilo += rows[i].inputKilo ? parseFloat(rows[i].inputKilo) : 0;
                inputMoney += rows[i].inputMoney ? parseFloat(rows[i].inputMoney) : 0;
            }
            $(this).datagrid('reloadFooter', [{
                orderDate: '合计',
                boxAmount:boxAmount.toFixed(1),
                inputKilo:inputKilo.toFixed(1),
                inputMoney:inputMoney.toFixed(2)
            }])
        },
    })
}

//白坯库存
function edatagrid_04() {
    $('#v_grid_bp').edatagrid({
        method: 'POST',
        param: {},
        showFooter:true,
        fit:true,
        columns: [[
            {
                field: 'client',
                title: '客户',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: false}}
            }, {
                field: 'batchNum',
                title: '批次号',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'planCode',
                title: '计划单号',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'specification',
                title: '规格',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'ps',
                title: '匹数',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }, {
                field: 'qty',
                title: '重量',
                width: 100,
                align: 'center',
                halign: 'center',
                sortable: true,
                editor: {type: 'textbox', options: {required: true}}
            }
        ]],
        onLoadSuccess: function (data) {
            let rows = data.rows;
            var ps=0;
            var qty=0;
            for (let i = 0; i < rows.length; i++) {
                ps += rows[i].ps ? parseFloat(rows[i].ps) : 0;
                qty += rows[i].qty ? parseFloat(rows[i].qty) : 0;
            }
            $(this).datagrid('reloadFooter', [{
                client: '合计',
                ps:ps.toFixed(1),
                qty:qty.toFixed(1)
            }])
        },
    })
}

function setParam(data) {
    $('#setting_param').form('load', data);
    $('#setting_param').find('.easyui-checkbox').each((index, item) => {
        let inputName = $(item).next('span').find('input').attr('name');
        if (data[inputName] == 1) $(item).checkbox('check');
        else $(item).checkbox('uncheck');
    })
}

//  结转
function btn_save() {
    //  参数设置
    $.messager.confirm({
        title: '提示',
        msg: '是否结转选中的数据库?',
        fn: function (r) {
            if (r) {
                $.ajax({
                    url: 'sqlBalance/backupsAndbalance',
                    traditional: true,
                    type: 'POST',
                    dataType: 'json',
                    success: function (data) {
                        $.messager.progress('close'); // 关闭加载对话框
                        if (data) $.messager.show({ title: '成功', msg: data.message });},
                    error: function () {
                        $.messager.progress('close'); // 关闭加载对话框
                        $.messager.show({ title: '错误', msg: '请求失败，请重试。' });
                    }
                });
            }
        }
    })
}

//备份
function btn_backups() {
    //  参数设置
    $.messager.confirm({
        title: '提示',
        msg: '是否备份数据库?',
        fn: function (r) {
            if (r) {
                $.messager.progress({ title: '请稍候', msg: '正在处理中...' }); // 显示加载对话框
                $.ajax({
                    url: 'sqlBalance/backups',
                    traditional: true,
                    type: 'POST',
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            $.messager.progress('close'); // 关闭加载对话框
                            $.messager.show({title: '成功', msg: data.message});
                        }
                    },
                    error: function () {
                        $.messager.progress('close'); // 关闭加载对话框
                        $.messager.show({ title: '错误', msg: '请求失败，请重试。' });
                    }
                })
            }
        }
    })
}

//恢复
function btn_recover() {
    //  参数设置
    $.messager.confirm({
        title: '提示',
        msg: '是否恢复数据库?',
        fn: function (r) {
            if (r) {
                $.messager.progress({ title: '请稍候', msg: '正在处理中...' }); // 显示加载对话框
                $.ajax({
                    url: 'sqlBalance/recover',
                    traditional: true,
                    type: 'POST',
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            $.messager.progress('close'); // 关闭加载对话框
                            $.messager.show({title: '成功', msg: data.message});
                        }
                    },
                    error: function () {
                        $.messager.progress('close'); // 关闭加载对话框
                        $.messager.show({ title: '错误', msg: '请求失败，请重试。' });
                    }
                })
            }
        }
    })
}

function getTabSelectedIndex() {
    return $('#tabs').tabs('getTabIndex', $('#tabs').tabs('getSelected'));
}